package com.fmdrama.fetcher;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;

import com.fmdrama.Main;
import com.fmdrama.bean.Profile;
import com.fmdrama.util.Configuration;
import com.fmdrama.util.LOG;

/**
 * 列表抓取器
 */
public class ListFetcher {
	private String[] cates = new String[]{"4", "1", "2", "3", "5", "7"};
	private HttpClient client = new HttpClient();
	private List<Profile> profiles = new ArrayList<Profile>();
	private int category = 0; //当前抓取的种类
	private int currPage = 0; //当前抓取的页码
	private int totalPage = 2; //总页数,从页面中解析总页数
	private long currTime = System.currentTimeMillis();
	private int count = 0;
	/**
	 * 抓取列表
	 * @return
	 */
	public List<Profile> fetch(int category){
		this.category = category;
		profiles.clear();
		while(currPage < totalPage && Main.self.cateStarted){
			currPage++;
			PostMethod getMethod = null;
			try{
				getMethod = new PostMethod("http://www.koreayh.com/vlist/" + category + "/" + currPage);
				client.executeMethod(getMethod);
				String result = getMethod.getResponseBodyAsString();
				parse(result);
				LOG.info("第" + currPage + "页");
				try {
					long interval = Configuration.getLong("fetch.interval", 0);
					if(interval > 0){  //抓取间隔时间大于0，就暂停指定时间
						Thread.sleep(interval);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}catch (Exception e) {
				e.printStackTrace();
			}finally{
				getMethod.releaseConnection();
			}
		}
		return profiles;
	}
	
	/**
	 * 解析，主要理由正则表达式，获取我们想要的数据
	 * @param source
	 * @return
	 */
	private List<Profile> parse(String source){
		source = source.substring(source.indexOf("class=\"filmListShow\""), source.indexOf("<p></p>"));//截取出需要的部分
		Matcher pageMatcher = Pattern.compile("<a href=\"/vlist/(\\d+)/(\\d+)/24\">끝페지</a>").matcher(source); //获得总页数
		if(pageMatcher.find()){
			try{
				int num = Integer.parseInt(pageMatcher.group(2));
				if(num > totalPage){
					totalPage = num;
				}
			}catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		Matcher blockMatcher = Pattern.compile("<li>[\\s\\S]*?</li>").matcher(source);
		Pattern pattern = Pattern.compile("href=\"/play/(\\d+)/(.*?)\"");
		Pattern patternDoc5 = Pattern.compile("출&nbsp;&nbsp;연：</span>(.*)");
		Pattern patternView = Pattern.compile("num_textformat.*?>(.*?)</font>");
		while(blockMatcher.find()){
			String sub = blockMatcher.group(0);
			Matcher matcher = pattern.matcher(sub);
			if(matcher.find()){
				try{
					count++;
					Profile profile = new Profile();
					profile.setId(Long.parseLong(matcher.group(1)));
					profile.setTitle(matcher.group(2));
					profile.setCategory(cates[category-2]);
					profile.setFtime(new Date(currTime - count*1000));
					
					Matcher matcherTemp = patternDoc5.matcher(sub);
					if(matcherTemp.find()){
						profile.setDoc5(matcherTemp.group(1));
					}
					matcherTemp = patternView.matcher(sub);
					if(matcherTemp.find()){
						profile.setView(Integer.parseInt(matcherTemp.group(1)));
					}
					LOG.info("解析 -> " + profile.toString());
					profiles.add(profile);
				}catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return profiles;
	}
	
	public void close(){
		client.getHttpConnectionManager().closeIdleConnections(0);
	}
	
}
